
import processing.core.*;

public class DataParser {
	
//	String filename; 
	
	int rowCount;
	String[] rowNames;
	int columnCount;
	String[] columnNames;
	float[][] data;
	String[] names;
	String[] altNames1;
	String[] altNames2;

	String[] altNames3;
	String[] altNames4;
	String[] altNames5;
	String[] altNames6;
	String[] altNames7;
	String[] altNames8;
	
	DataParser(String filename){
		 
		String[] rows = GLOBAL.processing.loadStrings(filename);
	    
	    String[] columns = PApplet.split(rows[0],'	');
	    columnNames = PApplet.subset(columns, 0); //only for determining number of columns
	    
	    columnCount = columnNames.length; 

	    rowNames = new String[rows.length]; //unique key should always be in first column
	    data = new float[rows.length][];
	    names = new String[rows.length];
	    altNames1 = new String[rows.length];
	    altNames2 = new String[rows.length];
	    
	    altNames3 = new String[rows.length];
	    altNames4 = new String[rows.length];
		altNames5 = new String[rows.length];
		altNames6 = new String[rows.length];
		altNames7 = new String[rows.length];
		altNames8 = new String[rows.length];

	    // start reading at row 0
	    for (int i = 0; i < rows.length; i++) {
	      
	      // split the row on the tabs
	      String[] pieces = PApplet.split(rows[i], '	');	      
	      
	      // copy row title
	      rowNames[rowCount] = pieces[0];
	      // copy data into the table starting at pieces[1]
	      data[rowCount] = PApplet.parseFloat(PApplet.subset(pieces, 2, 2));
//	      float[] altNamesTemp = PApplet.parseString(PApplet.subset(pieces, 1, 1));
	      altNames1[rowCount] = pieces[4];
	      altNames2[rowCount] = pieces[5];
	      
	      names[rowCount] = pieces[1];
	      
	      if (pieces.length > 6){
	    	  //System.out.println("The file "+filename+" has more than 6 columns"); 
	    	  altNames3[rowCount] = pieces[6];
	    	  if(pieces.length > 7){
	    		  altNames4[rowCount] = pieces[7];
	    		  altNames5[rowCount] = pieces[8];
	    		  altNames6[rowCount] = pieces[9];
	    		  altNames7[rowCount] = pieces[10];
	    		  altNames8[rowCount] = pieces[11];
	    	  }
	    	  
	      }
	      //

	      // increment the number of valid rows found so far
	      rowCount++;      
	    }
	    // resize the 'data' array as necessary
	    data = (float[][]) PApplet.subset(data, 0, rowCount);	
	}
	

	int getRowCount() {
	    return rowCount;
	}

	String getRowName(int rowIndex) {
	  return rowNames[rowIndex];
    }


    String[] getRowNames() {
	  return rowNames; //returns index
	}
	  
	int getRowIndex(String name) {
      for (int i = 0; i < rowCount; i++) {
		if (rowNames[i].equals(name)) {
		  return i;
		}
      }
      return -1;
	}


	int getColumnCount() {
		return columnCount;
	}


	String getColumnName(int colIndex) {
		return columnNames[colIndex];
	}


	String[] getColumnNames() {
		return columnNames;
	}


	float getFloat(int rowIndex, int col) {
		return data[rowIndex][col];
	}
	
	float getLatitude(int rowIndex){
		return data[rowIndex][0];
	}
	
	float getLongitude(int rowIndex){
		return data[rowIndex][1];
	}
	
	
	
	String getName(int rowIndex){
		return names[rowIndex];
	}
	
	String getAltNames1(int rowIndex){
		return altNames1[rowIndex];
	}
	
	String getAltNames2(int rowIndex){
		return altNames2[rowIndex];
	}
	
	String getAltNames3(int rowIndex){
		return altNames3[rowIndex];
	}
	
	String getAltNames4(int rowIndex){
		return altNames4[rowIndex];
	}
	
	String getAltNames5(int rowIndex){
		return altNames5[rowIndex];
	}
	
	String getAltNames6(int rowIndex){
		return altNames6[rowIndex];
	}
	
	String getAltNames7(int rowIndex){
		return altNames7[rowIndex];
	}
	
	String getAltNames8(int rowIndex){
		return altNames8[rowIndex];
	}
	
	boolean isValid(int row, int col) {
		if (row < 0) return false;
		if (row >= rowCount) return false;
		if (col >= columnCount) return false;
		if (col >= data[row].length) return false;
		if (col < 0) return false;
		return !Float.isNaN(data[row][col]);
	}


	float getColumnMin(int col) {
		float m = Float.MAX_VALUE;
		for (int row = 0; row < rowCount; row++) {
			if (isValid(row, col)) {
			   if (data[row][col] < m) {
			     m = data[row][col];
			   }
	        }
		}
		return m;
	}


	float getColumnMax(int col) {
		float m = -Float.MAX_VALUE;
		for (int row = 0; row < rowCount; row++) {
			if (isValid(row, col)) {
			   if (data[row][col] > m) {
			      m = data[row][col];
			   }
			}
		}
		return m;
	}
	
	float getRowMin(int row) {
	    float m = Float.MAX_VALUE;
	    for (int col = 0; col < columnCount; col++) {
	      if (isValid(row, col)) {
	        if (data[row][col] < m) {
	          m = data[row][col];
	        }
	      }
	    }
	    return m;
	  }
	
	float getRowMax(int row) {
	    float m = -Float.MAX_VALUE;
	    for (int col = 0; col < columnCount; col++) {
	      if (isValid(row, col)) {
	        if (data[row][col] > m) {
	          m = data[row][col];
	        }
	      }
	    }
	    return m;
	  }
	
	float getTableMin() {
	    float m = Float.MAX_VALUE;
	    for (int row = 0; row < rowCount; row++) {
	      for (int col = 0; col < columnCount; col++) {
	        if (isValid(row, col)) {
	          if (data[row][col] < m) {
	            m = data[row][col];
	          }
	        }
	      }
	    }
	    return m;
	  }
	
	float getTableMax() {
	    float m = -Float.MAX_VALUE;
	    for (int row = 0; row < rowCount; row++) {
	      for (int col = 0; col < columnCount; col++) {
	        if (isValid(row, col)) {
	          if (data[row][col] > m) {
	            m = data[row][col];
	          }
	        }
	      }
	    }
	    return m;
	  }
}